﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmBophan.
	/// </summary>
	public class frmBophan : System.Windows.Forms.Form
	{
		private AccessData  mDB=new AccessData();
        Language lan = new Language();
        string m_user = "";
        int co = 0;
        private DataSet ds = new DataSet();
        private System.Windows.Forms.DataGrid dgrDanhMuc;
        private Janus.Windows.EditControls.UIButton btn_them;
        private Janus.Windows.EditControls.UIButton btn_sua;
        private Janus.Windows.EditControls.UIButton btn_luu;
        private Janus.Windows.EditControls.UIButton btn_boqua;
        private Janus.Windows.EditControls.UIButton btn_xoa;
        private Janus.Windows.EditControls.UIButton uiButton6;
        private Janus.Windows.EditControls.UIButton btn_thoat;
        private Panel panel1;
        private Janus.Windows.EditControls.UIGroupBox uiGroupBox1;
        private TextBox txtTen;
        private TextBox txtMa;
        private Label label8;
        private Label label7;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmBophan()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmBophan));
            this.dgrDanhMuc = new System.Windows.Forms.DataGrid();
            this.btn_them = new Janus.Windows.EditControls.UIButton();
            this.btn_sua = new Janus.Windows.EditControls.UIButton();
            this.btn_luu = new Janus.Windows.EditControls.UIButton();
            this.btn_boqua = new Janus.Windows.EditControls.UIButton();
            this.btn_xoa = new Janus.Windows.EditControls.UIButton();
            this.uiButton6 = new Janus.Windows.EditControls.UIButton();
            this.btn_thoat = new Janus.Windows.EditControls.UIButton();
            this.panel1 = new System.Windows.Forms.Panel();
            this.uiGroupBox1 = new Janus.Windows.EditControls.UIGroupBox();
            this.txtTen = new System.Windows.Forms.TextBox();
            this.txtMa = new System.Windows.Forms.TextBox();
            this.label8 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.uiGroupBox1)).BeginInit();
            this.uiGroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // dgrDanhMuc
            // 
            this.dgrDanhMuc.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrDanhMuc.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrDanhMuc.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrDanhMuc.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrDanhMuc.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc.CaptionVisible = false;
            this.dgrDanhMuc.DataMember = "";
            this.dgrDanhMuc.FlatMode = true;
            this.dgrDanhMuc.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrDanhMuc.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrDanhMuc.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrDanhMuc.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc.LinkColor = System.Drawing.Color.Teal;
            this.dgrDanhMuc.Location = new System.Drawing.Point(0, -5);
            this.dgrDanhMuc.Name = "dgrDanhMuc";
            this.dgrDanhMuc.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc.RowHeaderWidth = 10;
            this.dgrDanhMuc.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrDanhMuc.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc.Size = new System.Drawing.Size(784, 374);
            this.dgrDanhMuc.TabIndex = 4;
            this.dgrDanhMuc.CurrentCellChanged += new System.EventHandler(this.dgrDanhmuc_CurrentCellChanged);
            // 
            // btn_them
            // 
            this.btn_them.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_them.Image = ((System.Drawing.Image)(resources.GetObject("btn_them.Image")));
            this.btn_them.Location = new System.Drawing.Point(228, 5);
            this.btn_them.Name = "btn_them";
            this.btn_them.Size = new System.Drawing.Size(75, 26);
            this.btn_them.TabIndex = 116;
            this.btn_them.Text = "Mới";
            this.btn_them.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_them.Click += new System.EventHandler(this.btn_them_Click);
            // 
            // btn_sua
            // 
            this.btn_sua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_sua.Image = ((System.Drawing.Image)(resources.GetObject("btn_sua.Image")));
            this.btn_sua.Location = new System.Drawing.Point(307, 5);
            this.btn_sua.Name = "btn_sua";
            this.btn_sua.Size = new System.Drawing.Size(75, 26);
            this.btn_sua.TabIndex = 0;
            this.btn_sua.Text = "Sửa";
            this.btn_sua.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_sua.Click += new System.EventHandler(this.btn_sua_Click);
            // 
            // btn_luu
            // 
            this.btn_luu.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_luu.Image = ((System.Drawing.Image)(resources.GetObject("btn_luu.Image")));
            this.btn_luu.Location = new System.Drawing.Point(386, 5);
            this.btn_luu.Name = "btn_luu";
            this.btn_luu.Size = new System.Drawing.Size(75, 26);
            this.btn_luu.TabIndex = 0;
            this.btn_luu.Text = "Lưu";
            this.btn_luu.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_luu.Click += new System.EventHandler(this.btn_luu_Click);
            // 
            // btn_boqua
            // 
            this.btn_boqua.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_boqua.Image = ((System.Drawing.Image)(resources.GetObject("btn_boqua.Image")));
            this.btn_boqua.Location = new System.Drawing.Point(465, 5);
            this.btn_boqua.Name = "btn_boqua";
            this.btn_boqua.Size = new System.Drawing.Size(75, 26);
            this.btn_boqua.TabIndex = 119;
            this.btn_boqua.Text = "Bỏ qua";
            this.btn_boqua.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_boqua.Click += new System.EventHandler(this.btn_boqua_Click);
            // 
            // btn_xoa
            // 
            this.btn_xoa.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_xoa.Image = ((System.Drawing.Image)(resources.GetObject("btn_xoa.Image")));
            this.btn_xoa.Location = new System.Drawing.Point(544, 5);
            this.btn_xoa.Name = "btn_xoa";
            this.btn_xoa.Size = new System.Drawing.Size(75, 26);
            this.btn_xoa.TabIndex = 120;
            this.btn_xoa.Text = "Xóa";
            this.btn_xoa.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_xoa.Click += new System.EventHandler(this.btn_xoa_Click);
            // 
            // uiButton6
            // 
            this.uiButton6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.uiButton6.Image = ((System.Drawing.Image)(resources.GetObject("uiButton6.Image")));
            this.uiButton6.Location = new System.Drawing.Point(623, 5);
            this.uiButton6.Name = "uiButton6";
            this.uiButton6.Size = new System.Drawing.Size(75, 26);
            this.uiButton6.TabIndex = 121;
            this.uiButton6.Text = "In";
            this.uiButton6.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            // 
            // btn_thoat
            // 
            this.btn_thoat.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btn_thoat.Image = ((System.Drawing.Image)(resources.GetObject("btn_thoat.Image")));
            this.btn_thoat.Location = new System.Drawing.Point(702, 5);
            this.btn_thoat.Name = "btn_thoat";
            this.btn_thoat.Size = new System.Drawing.Size(75, 26);
            this.btn_thoat.TabIndex = 122;
            this.btn_thoat.Text = "Thoát";
            this.btn_thoat.VisualStyle = Janus.Windows.UI.VisualStyle.Office2007;
            this.btn_thoat.Click += new System.EventHandler(this.btn_thoat_Click);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btn_thoat);
            this.panel1.Controls.Add(this.btn_them);
            this.panel1.Controls.Add(this.uiButton6);
            this.panel1.Controls.Add(this.btn_sua);
            this.panel1.Controls.Add(this.btn_xoa);
            this.panel1.Controls.Add(this.btn_luu);
            this.panel1.Controls.Add(this.btn_boqua);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 427);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(784, 35);
            this.panel1.TabIndex = 123;
            // 
            // uiGroupBox1
            // 
            this.uiGroupBox1.Controls.Add(this.txtTen);
            this.uiGroupBox1.Controls.Add(this.txtMa);
            this.uiGroupBox1.Controls.Add(this.label8);
            this.uiGroupBox1.Controls.Add(this.label7);
            this.uiGroupBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.uiGroupBox1.Location = new System.Drawing.Point(0, 375);
            this.uiGroupBox1.Name = "uiGroupBox1";
            this.uiGroupBox1.Size = new System.Drawing.Size(784, 52);
            this.uiGroupBox1.TabIndex = 124;
            this.uiGroupBox1.VisualStyle = Janus.Windows.UI.Dock.PanelVisualStyle.Office2007;
            // 
            // txtTen
            // 
            this.txtTen.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtTen.Location = new System.Drawing.Point(317, 21);
            this.txtTen.Name = "txtTen";
            this.txtTen.Size = new System.Drawing.Size(455, 20);
            this.txtTen.TabIndex = 1;
            // 
            // txtMa
            // 
            this.txtMa.Location = new System.Drawing.Point(91, 21);
            this.txtMa.Name = "txtMa";
            this.txtMa.Size = new System.Drawing.Size(119, 20);
            this.txtMa.TabIndex = 0;
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(229, 26);
            this.label8.Name = "label8";
            this.label8.Size = new System.Drawing.Size(86, 13);
            this.label8.TabIndex = 1;
            this.label8.Text = "Tên phòng ban :";
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(6, 26);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(82, 13);
            this.label7.TabIndex = 0;
            this.label7.Text = "Mã phòng ban :";
            // 
            // frmBophan
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(784, 462);
            this.Controls.Add(this.uiGroupBox1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.dgrDanhMuc);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "frmBophan";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục phòng ban";
            this.Load += new System.EventHandler(this.frmBophan_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc)).EndInit();
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.uiGroupBox1)).EndInit();
            this.uiGroupBox1.ResumeLayout(false);
            this.uiGroupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		
		private void Ena_control(bool ena)
		{
			btn_them.Enabled=ena;
			btn_sua.Enabled=ena;
			btn_luu.Enabled=!ena;
			btn_boqua.Enabled=!ena;
			btn_xoa.Enabled=ena;
			btn_thoat.Enabled=ena;
		}
		private void Ena_textbox(bool ena)
		{            
            txtTen.Enabled=ena;			
		}
		private void clear_Text()
		{			
			txtTen.Clear();
		}
		private void refresh_Text()
		{
            try
            {
                string a = "", kv = "";
                int i = dgrDanhMuc.CurrentRowIndex;
                if (i >= 0)
                {
                    txtMa.Text = dgrDanhMuc[i, 0].ToString();
                    txtTen.Text = dgrDanhMuc[i, 1].ToString();
                }
            }
            catch { }  		
		}
		private void frmBophan_Load(object sender, System.EventArgs e)
		{
			bophan();			
			Ena_control(true);
            btn_sua.Enabled = false;
            btn_xoa.Enabled = false;
			refresh_Text();
			Ena_textbox(false);
			btn_them.Focus();
		}
		private void bophan()
		{
            string sql = "";
            sql=" SELECT madv,tenbp ";
	        sql += " FROM " + mDB.user + ".bophan";
            DataSet ds1=new DataSet();
			ds1=mDB.get_data(sql);
			dgrDanhMuc.DataSource=null;
			dgrDanhMuc.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ds1.Tables[0].TableName;
		
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "madv";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã bộ phận");
			TextCol1.Width = 80;
			TextCol1.ReadOnly=true;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc.TableStyles.Add(ts);
		
			DataGridTextBoxColumn TextCol2=new DataGridTextBoxColumn();
			TextCol2.MappingName = "tenbp";
			TextCol2.HeaderText = lan.Change_language_MessageText("Tên bộ phận");
			TextCol2.Width = 180;
			TextCol2.ReadOnly=true;
			TextCol2.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol2);
			dgrDanhMuc.TableStyles.Add(ts);	

			DataGridTextBoxColumn TextCol3=new DataGridTextBoxColumn();
			TextCol3.MappingName = "masoft";
			TextCol3.HeaderText = lan.Change_language_MessageText("Nhóm khoa");
			TextCol3.Width = 120;
			TextCol3.ReadOnly=true;
			TextCol3.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol3);
			dgrDanhMuc.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol4 = new DataGridTextBoxColumn();
            TextCol4.MappingName = "sothutu";
            TextCol4.HeaderText = lan.Change_language_MessageText("Số TT");
            TextCol4.Width = 40;
            TextCol4.ReadOnly = true;
            TextCol4.Alignment = HorizontalAlignment.Center;
            ts.GridColumnStyles.Add(TextCol4);
            dgrDanhMuc.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol5 = new DataGridTextBoxColumn();
            TextCol5.MappingName = "sogiuong";
            TextCol5.HeaderText = lan.Change_language_MessageText("Số Giường");
            TextCol5.Width = 40;
            TextCol5.ReadOnly = true;
            TextCol5.Alignment = HorizontalAlignment.Center;
            ts.GridColumnStyles.Add(TextCol5);
            dgrDanhMuc.TableStyles.Add(ts);

            DataGridTextBoxColumn TextCol6 = new DataGridTextBoxColumn();
            TextCol6.MappingName = "khuvuc";
            TextCol6.HeaderText = lan.Change_language_MessageText("Khu vực");
            TextCol6.Width = 100;
            TextCol6.ReadOnly = true;
            TextCol6.Alignment = HorizontalAlignment.Center;
            ts.GridColumnStyles.Add(TextCol6);
            dgrDanhMuc.TableStyles.Add(ts);

			ts.DataGrid.DataSource=ds1.Tables[0];
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrDanhMuc.DataSource, dgrDanhMuc.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowNew = false;
            dv.AllowEdit = false;
            dv.AllowDelete = false;
		}

		private void dgrDanhmuc_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				refresh_Text();
				Ena_textbox(false);
				Ena_control(true);
                btn_them.Enabled = false;
                btn_boqua.Enabled = true;
			}
			catch{}
		}

		private void txtTen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
            if (e.KeyCode == Keys.Enter)
			{				
				SendKeys.Send("{Tab}");
			}
		}

		private void btnLuu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				btn_them.Focus();
			}
		}

		private void btnKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}	
       

        private bool kemtra(string madv)
        {
            bool flag = false;
            try
            {
                flag = mDB.get_data("select count(*) as tong from " + mDB.user + ".llnv where madv='" + madv + "'").Tables[0].Rows[0][0].ToString().Equals("0");
            }
            catch
            {
                flag = false;
            }
            if (!flag)
                MessageBox.Show(lan.Change_language_MessageText("Bô phận đã có nhân viên không thể hủy!"),AccessData.Msg);
            return flag;
        }
        private string get_maxid()
        {
            int m_tam;
            ds = mDB.get_data("SELECT Max(to_number(madv,999)) FROM " + m_user + ".bophan");
            if (ds.Tables[0].Rows[0][0].ToString() == "")
            {
                m_tam = 1;
            }
            else
            {
                m_tam = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                m_tam += 1;
            }
             return m_tam.ToString().PadLeft(3, '0').ToString();//txtsott.Text = txtMa.Text
        }
        

        private void btn_them_Click(object sender, EventArgs e)
        {
            co = 0;
            Ena_control(false);
            Ena_textbox(true);
            clear_Text();
            m_user = mDB.user;
            txtMa.Text = get_maxid();
            txtTen.Focus();
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            co = 1;
            Ena_control(false);
            Ena_textbox(true);
            txtTen.Focus();
        }

        private void btn_luu_Click(object sender, EventArgs e)
        {
            int m_id = 0;
            try
            {
                string a = "", kv = "";
                if (co == 0)
                {
                    m_id = Int32.Parse(get_maxid());
                    txtMa.Text = m_id.ToString().PadLeft(3, '0');
                }
                else
                {
                    if (txtMa.Text != "")
                        m_id = Int32.Parse(txtMa.Text.ToString());
                }
                mDB.Insert_DMBOPHAN(txtMa.Text, txtTen.Text);
                bophan();
                co = 0;
                Ena_control(true);
                btn_xoa.Enabled = false;
                btn_sua.Enabled = false;
                Ena_textbox(false);
                btn_them.Focus();
            }
            catch { }
        }

        private void btn_boqua_Click(object sender, EventArgs e)
        {
            Ena_control(true);
            Ena_textbox(false);
            btn_xoa.Enabled = false;
            btn_sua.Enabled = false;
            txtTen.Enabled = false;
            refresh_Text();
            btn_thoat.Focus();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ bộ phận này?"), AccessData.Msg, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes && kemtra(txtMa.Text.Trim()))
            {
                mDB.get_data("DELETE  FROM " + mDB.user + ".bophan WHERE madv='" + txtMa.Text.Trim() + "'");
            }
            bophan();
            refresh_Text();
            btn_xoa.Enabled = false;
            btn_sua.Enabled = false;
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
	}
}
